package com.orderplus.opshop.common.util;

import com.orderplus.opshop.common.util.date.DateUtil;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * Title: 生成随机数
 * Description:
 * Company: Copyright @ 2019 OrderPlus 版权所有
 */
public class RandomUtil {

	public static String getRandomStr(int size) {
		return ValueUtil.toStr(getRandomNumber(size));
	}

	//生成 指定 多少 位随机数    size:长度 ,最大支持10位
	public static Integer getRandomNumber(int size) {
		//生成 指定 多少位随机数
		Integer randomNum = (int) ((Math.random() * 9 + 1) * Math.pow(10, size - 1));
		return randomNum;
	}

	public static Integer generateCode() {
		Double result = Math.random() * 1000;
		return result.intValue();
	}

	public static String generateLetter(int number) {
		String str = "";
		for (int i = 0; i < number; i++) {
			str = str + (char) (Math.random() * 26 + 'A');
		}
		return str;
	}

	public static String generateNumber(int number) {
		Double result = Math.random() * number;
		String format = String.format("%02d", result.intValue());
		return format;
	}

	public static void main(String[] args) {
		for (int i = 0; i < 100000000; i++) {
//			int number = getRandomNumber(1);

//			System.out.println(i+"数量: "+number);
			Date now = new Date();

//			System.out.println(i+"now: "+now);

			Date optime = DateUtil.addSecond(now, ValueUtil.toInt(RandomUtil.getRandomNumber(1)));

//			System.out.println(i+"optime: "+optime);

			int compare = DateUtil.compareDate(now, optime);
			if (compare != -1) {
				System.out.println("异常情况");
			}
		}

	}

	/**
	 * 打乱列表实现方法
	 *
	 * @param list
	 * @param <T>
	 */
	public static <T> void shuffle(List<T> list) {
		int size = list.size();
		Random random = new Random();

		for (int i = 0; i < size; i++) {
			// 获取随机位置
			int randomPos = random.nextInt(size);
			// 当前元素与随机元素交换
			T temp = list.get(i);
			list.set(i, list.get(randomPos));
			list.set(randomPos, temp);
		}
	}

}
